//
//  DataSqlite.m
//  dddddemo
//
//  Created by lanou3g on 15/11/5.
//  Copyright © 2015年 zhk. All rights reserved.
//

#import "DataSqlite.h"
#import "sqlite3.h"
#import "ModelFirst.h"
static DataSqlite * ds = nil;


@implementation DataSqlite

+ (instancetype)shareDataSqlite
{
    if (ds == nil) {
        static dispatch_once_t token;
        dispatch_once(&token, ^{
            ds = [[DataSqlite alloc]init];
        });
    }
    return ds;
}

static sqlite3 * sql1 = nil;

- (void)openDataSqlite
{
    NSString * documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSString * dsPath = [documentPath stringByAppendingString:@"/diary.sqlite"];
    NSLog(@"%@",dsPath);
    int temp = sqlite3_open(dsPath.UTF8String, &sql1);
    if (temp == SQLITE_OK) {
        NSLog(@"数据库打开成功");
    }else
    {
        NSLog(@"数据库打开失败");
    }
}

- (void)creatTable
{
    NSString * sqlStr = @"CREATE TABLE IF NOT EXISTS REME (title TEXT PRIMARY KEY NOT NULL, content TEXT,image blob,currentTime TEXT,image2 blob,image3 blob,ReFilePath TEXT)";
    int temp = sqlite3_exec(sql1, sqlStr.UTF8String, NULL, NULL, NULL);
    if (temp == SQLITE_OK) {
        NSLog(@"创建成功");
    }else
    {
        NSLog(@"创建失败");
    }
}

- (void)insertTitle:(NSString *)title Content:(NSString *)content image:(UIImage *)image currentTime:(NSString *)currentTime image:(UIImage *)image2 image:(UIImage *)image3 filePath:(NSString *)path;
{
    NSData * data1 = UIImagePNGRepresentation(image);
    NSData * data2 = UIImagePNGRepresentation(image2);
    NSData * data3 = UIImagePNGRepresentation(image3);
    NSString * sqlStr = [NSString stringWithFormat:@"INSERT INTO REME(title,content,image,currentTime,image2,image3,ReFilePath) VALUES(?,?,?,?,?,?,?)"];
    sqlite3_stmt * stmt = nil;
    int temp = sqlite3_prepare_v2(sql1, sqlStr.UTF8String, -1, &stmt, NULL);
    if (temp == SQLITE_OK) {
        sqlite3_bind_text(stmt, 1, title.UTF8String, -1, NULL);
        sqlite3_bind_text(stmt, 2, content.UTF8String, -1, NULL);
        sqlite3_bind_blob(stmt, 3, data1.bytes, (int)data1.length, NULL);
        sqlite3_bind_text(stmt, 4, currentTime.UTF8String, -1, NULL);
        sqlite3_bind_text(stmt, 5, data2.bytes, (int)data2.length, NULL);
        sqlite3_bind_text(stmt, 6, data3.bytes, (int)data3.length, NULL);
        sqlite3_bind_text(stmt, 7, path.UTF8String, -1, NULL);
        if (sqlite3_step(stmt) == SQLITE_DONE) {
            NSLog(@"插入成功");
        } else {
            NSLog(@"插入失败");
        }
    }
    sqlite3_finalize(stmt);
}

- (NSArray *)findAll
{
    NSString * sqlStr = [NSString stringWithFormat:@"SELECT * FROM REME"];
    sqlite3_stmt * stmt = nil;
    int temp = sqlite3_prepare_v2(sql1, sqlStr.UTF8String, -1, &stmt, NULL);
    NSMutableArray * dataArray = [NSMutableArray array];
    if (temp == SQLITE_OK) {
        // 判断是否有数据
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            NSString * tempTitle = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 0)];
            NSString * tempContent = [NSString stringWithUTF8String:(const char *) sqlite3_column_text(stmt, 1)];
            NSData * tempImageData = [NSData dataWithBytes:(const void *)sqlite3_column_blob(stmt, 2) length:sqlite3_column_bytes(stmt, 2)];
            NSString * tempTime = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 3)];
            NSData * tempImageData2 = [NSData dataWithBytes:(const void *)sqlite3_column_blob(stmt, 4) length:sqlite3_column_bytes(stmt, 4)];
            NSData * tempImageData3 = [NSData dataWithBytes:(const void *)sqlite3_column_blob(stmt, 5) length:sqlite3_column_bytes(stmt, 5)];
            NSString * tempPath = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 6)];
            
            ModelFirst *aModel = [[ModelFirst alloc] init];
            aModel.title = tempTitle;
            aModel.content = tempContent;
            aModel.imageData = tempImageData;
            aModel.currentTime = tempTime;
            aModel.imageData2 = tempImageData2;
            aModel.imageData3 = tempImageData3;
            aModel.ReFilePath = tempPath;
            [dataArray addObject:aModel];
        }
    }
    sqlite3_finalize(stmt);

    return dataArray;
}

- (ModelFirst *)findModelFirstByTitle:(NSString *)title
{
    NSString * sqlStr = [NSString stringWithFormat:@"SELECT * FROM REME WHERE title = '%@'",title];
    sqlite3_stmt * stmt = nil;
    int temp = sqlite3_prepare_v2(sql1, sqlStr.UTF8String, -1, &stmt, NULL);
    
    ModelFirst * model = [[ModelFirst alloc]init];
    if (temp == SQLITE_OK) {
        sqlite3_bind_text(stmt, 1, title.UTF8String, -1, NULL);
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            NSString * tempTitle = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 0)];
            NSString * tempContent = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 1)];
            NSData * tempImageData = [NSData dataWithBytes:(const void *)sqlite3_column_blob(stmt, 2) length:sqlite3_column_bytes(stmt, 2)];
            NSString * tempTime = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 3)];
            NSData * tempImageData2 = [NSData dataWithBytes:(const void *)sqlite3_column_blob(stmt, 4) length:sqlite3_column_bytes(stmt, 4)];
            NSData * tempImageData3 = [NSData dataWithBytes:(const void *)sqlite3_column_blob(stmt, 5) length:sqlite3_column_bytes(stmt, 5)];
            NSString * tempPath = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 6)];
            
            model.title = tempTitle;
            model.content = tempContent;
            model.imageData = tempImageData;
            model.currentTime = tempTime;
            model.imageData2 = tempImageData2;
            model.imageData3 = tempImageData3;
            model.ReFilePath = tempPath;
        }
    }
    sqlite3_finalize(stmt);
    return model;
}

- (void)deleteModelByTitle:(NSString *)title
{
    NSString * sqlStr = [NSString stringWithFormat:@"DELETE FROM REME WHERE title = '%@'",title];
    int temp = sqlite3_exec(sql1, sqlStr.UTF8String, NULL, NULL, NULL);
    if (temp == SQLITE_OK) {
        NSLog(@"删除成功");
    }else
    {
        NSLog(@"删除失败");
    }
}

- (BOOL)findExistByTile:(NSString *)title
{
    NSString * sqlStr = [NSString stringWithFormat:@"SELECT * FROM REME WHERE title='%@'",title];
    sqlite3_stmt *stmt = nil;
    int temp = sqlite3_prepare_v2(sql1, sqlStr.UTF8String, -1, &stmt, NULL);
    if (temp == SQLITE_OK) {
        sqlite3_bind_text(stmt, 0, title.UTF8String, -1, NULL);
        if (sqlite3_step(stmt) == SQLITE_ROW) {
            // 存在一样的标题
            return NO;
        }
    }
    return YES;
}

@end
